const Mock = require('mockjs');

const tokens = {
  admin: {
    token: 'admin-token',
  },
  editor: {
    token: 'editor-token',
  },
};

const users = {
  'admin-token': {
    roles: ['admin'],
    introduction: 'I am a super administrator',
    avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
    name: 'Super Admin',
  },
  'editor-token': {
    roles: ['editor'],
    introduction: 'I am an editor',
    avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
    name: 'Normal Editor',
  },
};

module.exports = [
  {
    url: '/admin/product/baseTrademark/.*',
    type: 'get',
    response: (config) => {
      // 1. 获取完整 URL
      const url = config.url; // 如：/admin/product/baseTrademark/1/10

      // 2. 用正则提取 page 和 limit
      const match = url.match(/\/admin\/product\/baseTrademark\/(\d+)\/(\d+)/);
      if (!match) {
        return {
          code: 400,
          message: 'Invalid URL format',
        };
      }
      const page = parseInt(match[1]);
      const limit = parseInt(match[2]);

      // 3. 模拟分页数据
      // 模拟总数据（比如 100 条）
      const total = 100
      const totalPages = Math.ceil(total / limit)

      // 生成当前页的数据
      const data = Mock.mock({
        'items|10': [
          {
            // 假设每页最多返回 limit 条，这里用 10 示例
            'id|+1': (page - 1) * limit + 1,
            tmName: '@ctitle(4,8)',
            logoUrl: '@image("100x50", "@color", "@first")',
            createTime: '@datetime',
          },
        ],
      }).items.slice(0, limit); // 确保数量不超过 limit

      // mock error
      if (!data) {
        return {
          code: 50008,
          message: '获取数据失败',
        };
      }

      return {
        code: 200,
        message: 'success',
        data: {
          records: data,
          total,
          size: limit,
          current: page,
          pages: totalPages,
          searchCount: true,
        },
      };
    },
  },
];
